package com.qidian.tcba.admin.api.controller;

import com.qidian.tcba.admin.model.dto.parkLot.*;
import com.qidian.tcba.admin.model.query.ParkLotRecordManageQueryDTO;
import com.qidian.tcba.admin.model.dto.parkLotManage.ParkLotRecordManageResultDTO;
import com.qidian.tcba.admin.model.dto.parkLotRecord.*;
import com.qidian.tcba.admin.service.ParkLotRecordTaskService;
import com.qidian.tcba.utils.JSONResult;
import com.qidian.tcba.utils.Message;
import com.qidian.tcba.utils.Pageable;
import com.qidian.tcba.utils.dto.PageableRequestDTO;
import com.qidian.tcba.utils.result.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author:fyl
 * @date 2021/7/6 15:54
 * @Modified By:
 * @Modified Date:
 * @Description:
 */
@RestController
@RequestMapping("/parkLotRecordTask")
@Api(tags ="备案任务")
public class ParkLotRecordTaskController {
    @Autowired
    private ParkLotRecordTaskService parkLotRecordTaskService;


    @ApiOperation("经营者，我的备案")
    @GetMapping("/getParkStatusData/{userId}")
    public JsonResult parkStatusData(@PathVariable("userId") int userId) {
        ParkStatusDataDTO parkStatusData = parkLotRecordTaskService.getParkStatusData(userId);
        return JsonResult.success().data("row", parkStatusData);
    }

    @ApiOperation("经营者，获取备案列表")
    @GetMapping("/getParkRecordList/{status}/{userId}/{page}/{size}")
    public JSONResult<List<ParkRecordListDTO>> parkRecordList(@PathVariable("status") int status,
                                     @PathVariable("userId") int userId,
                                     @PathVariable("page") int page,
                                     @PathVariable("size") int size) {
        List<ParkRecordListDTO> parkRecordList = parkLotRecordTaskService.getParkRecordList(status, userId, page, size);
        int totalCount = parkLotRecordTaskService.getParkRecordNumber(userId);
        Pageable pageable = new PageableRequestDTO(page,size);
        return new JSONResult<>(parkRecordList, new Message("DB.SELECT_SUCCESS"),pageable,totalCount);
    }

    @ApiOperation("获取两条备案信息审核流程")
    @GetMapping("/getRecentRecordList/{userId}")
    public JsonResult getRecentRecordList(@PathVariable("userId") int userId) {
        List<RecentRecordStageDTO> recentRecordList = parkLotRecordTaskService.getRecentRecordList(userId);
        return JsonResult.success().data("rows", recentRecordList);
    }

    @ApiOperation("暂存")
    @PostMapping("/hold/{userId}")
    public JsonResult holdParkLotRecord(@RequestBody ParkLotSubmitDTO parkLotSubmitDTO,
                                        @PathVariable int userId) throws InvocationTargetException, IllegalAccessException {
        ParkLotInfoDTO parkLotInfo = parkLotRecordTaskService.holdParkRecord(parkLotSubmitDTO,userId);
        return JsonResult.success().data("row", parkLotInfo);
    }

    @ApiOperation("提交")
    @PostMapping("/submit/{userId}")
    public JsonResult submitParkLotRecord(@RequestBody ParkLotSubmitDTO parkLotSubmitDTO,
                                          @PathVariable int userId) throws InvocationTargetException, IllegalAccessException {
        parkLotRecordTaskService.submitParkRecord(parkLotSubmitDTO,userId);
        return JsonResult.success();
    }

    @ApiOperation("获取备案详情")
    @GetMapping("/parkLotTaskInfo/{id}")
    public JsonResult getParkLotInfo(@PathVariable("id") int id) {
        ParkLotInfoDTO parkLotInfo = parkLotRecordTaskService.getParkLotInfo(id);
        return JsonResult.success().data("rows", parkLotInfo);
    }

    @ApiOperation("获取整改信息")
    @GetMapping("/getReformInfo/{taskId}/{stage}")
    public JsonResult getReformInfo(@PathVariable("taskId") int taskId,@PathVariable int stage) {
        ParkLotReformDTO parkReformInfo = parkLotRecordTaskService.getParkReformInfo(taskId,stage);
        return JsonResult.success().data("row", parkReformInfo);
    }

    @ApiOperation("提交整改信息")
    @PostMapping("/submitReformInfo")
    public JsonResult submitReformInfo(@RequestBody ParkLotReformSubmitDTO parkLotReformSubmitDTO) throws InvocationTargetException, IllegalAccessException {
        if (parkLotRecordTaskService.submitReformInfo(parkLotReformSubmitDTO)) {
            return JsonResult.success();
        }
        return JsonResult.error();
    }

    @ApiOperation("根据id删除备案任务")
    @DeleteMapping("/delete/{id}")
    public JsonResult deleteParkLot(@PathVariable("id") int id){
        if (parkLotRecordTaskService.deleteParkLotRecord(id)) {
            return JsonResult.success();
        }else {
            return JsonResult.error();
        }
    }

    @ApiOperation("作废")
    @GetMapping("/cancellation/{taskId}")
    public JsonResult cancellationParkLot(@PathVariable("taskId") int taskId){
        parkLotRecordTaskService.cancellationParkLot(taskId);
        return JsonResult.success();
    }

    @ApiOperation("管理员，获取代办中心数据")
    @GetMapping("/getAgentCenterInfo/{distinctId}")
    public JSONResult<Map<Integer,AgentCenterDTO>> getAgentCenterInfo(@PathVariable("distinctId") int distinctId){
        Map<Integer, AgentCenterDTO> agentCenter = parkLotRecordTaskService.getAgentCenterDTOMap(distinctId);
        return new JSONResult<>(agentCenter,200,"成功");
    }

    @ApiOperation("管理员,获取审核列表")
    @GetMapping("/getManageAuditList/{stage}/{index}/{distinctId}/{page}/{size}")
    public JSONResult<List<ManageParkRecordListDTO>> getManageAuditList(@PathVariable int stage,
                                                                        @PathVariable int index,
                                                                        @PathVariable int distinctId,
                                                                        @PathVariable int page,
                                                                        @PathVariable int size ){
        List<ManageParkRecordListDTO> manageAuditList = parkLotRecordTaskService.getManageAuditList(stage, index, distinctId, page, size);
        return new JSONResult<>(manageAuditList, 200, "成功");
    }

    @ApiOperation("停车备案管理")
    @PostMapping("/getRecordManage")
    public JSONResult<List<ParkLotRecordManageResultDTO>> getManageResult(@RequestBody @Validated ParkLotRecordManageQueryDTO parkLotRecordManageQueryDTO) {
        List<ParkLotRecordManageResultDTO> manageResultList = parkLotRecordTaskService.getManageResultList(parkLotRecordManageQueryDTO);
        return new JSONResult<>(manageResultList, 200, "成功");
    }
}